<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Metrics Calculation</title>
  </head>
  <body>
    <script>
const values = [
  10, 0, 10, 10, 10, 10, 10, 10, 0, 10, 10, 10, 10, 0, 0, 10, 10, 10, 0, 10, 10, 10, 10, 10, 10, 10, 10, 10, 0, 10, 10,
  0, 10, 10, 10, 10, 10, 0, 10, 0, 10, 10, 10, 0, 10, 10, 10, 10, 10, 10, 10, 10, 0, 10, 10, 10, 10, 0, 0, 10, 10, 0, 0,
  10, 10, 10, 10, 10, 10, 0, 0, 10, 10, 10, 10, 10, 0, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 0, 10, 10, 10, 10,
  10, 10, 0, 0, 10, 10, 0, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 0, 10, 10, 10, 0, 10, 10, 10, 10, 10, 10, 10, 10,
  10, 0, 0, 10, 10, 10, 10, 0, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 0, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
  10, 0, 10, 10, 10, 10, 10, 0, 10, 10, 10, 10, 10, 0, 10, 0, 10, 10, 0, 10, 10, 10, 0, 0, 0, 10, 10, 10, 0, 10, 10, 10,
  0, 10, 10, 10, 0, 0, 10, 10, 10, 10, 10, 10, 0, 10, 10, 10, 10, 10, 0, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
  10, 10, 10, 10, 10, 10, 10, 0, 10, 10, 10, 10, 10, 10, 0, 10, 0, 10, 10, 10, 10, 10, 0, 10, 10, 0, 10, 10, 10, 10, 10,
  10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 0, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
  10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 0, 10, 10, 10, 10, 10, 0, 10,
  10, 10, 0, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 0, 10, 10, 10, 10, 0, 10, 10, 10, 0, 10, 10, 10, 10, 10, 10, 0, 10,
  10, 10, 10, 10, 0, 10, 10, 10, 0, 10, 10, 0, 0, 10, 10, 10, 10, 0, 0, 10, 0, 10, 10, 10, 10, 10
]

      function calculateMetrics(values) {
        // 初始化统计变量
        let totalErrors = 0;
        let totalCorrects = 0;

        let totalErrorStreaks = 0;
        let totalCorrectStreaks = 0;

        let sumCorrectBeforeError = 0;
        let countCorrectBeforeError = 0;

        let sumErrorBeforeCorrect = 0;
        let countErrorBeforeCorrect = 0;

        let currentErrorStreak = 0;
        let currentCorrectStreak = 0;

        let lastValue = null;

        // 单次遍历处理所有统计
        for (let i = 0; i < values.length; i++) {
          const value = values[i];

          // 输入验证
          if (value !== 0 && value !== 10) {
            console.warn(`发现非0或10的值: ${value} 在索引 ${i}`);
            continue; // 忽略非0或10的值
          }

          if (value === 0) {
            // 处理错误
            totalErrors += 1;

            if (lastValue !== 0) {
              totalErrorStreaks += 1; // 新的错误连续段开始
            }
            currentErrorStreak += 1;

            // 如果之前有正确的连续段，记录
            if (currentCorrectStreak > 0) {
              sumCorrectBeforeError += currentCorrectStreak;
              countCorrectBeforeError += 1;
              currentCorrectStreak = 0;
            }
          } else if (value === 10) {
            // 处理正确
            totalCorrects += 1;

            if (lastValue !== 10) {
              totalCorrectStreaks += 1; // 新的正确连续段开始
            }
            currentCorrectStreak += 1;

            // 如果之前有错误的连续段，记录
            if (currentErrorStreak > 0) {
              sumErrorBeforeCorrect += currentErrorStreak;
              countErrorBeforeCorrect += 1;
              currentErrorStreak = 0;
            }
          }

          lastValue = value;
        }

        // 计算平均值
        const averageConsecutiveErrors =
          totalErrorStreaks > 0
            ? parseFloat((totalErrors / totalErrorStreaks).toFixed(2))
            : 0;

        const averageConsecutiveCorrects =
          totalCorrectStreaks > 0
            ? parseFloat((totalCorrects / totalCorrectStreaks).toFixed(2))
            : 0;

        const averageCorrectBeforeError =
          countCorrectBeforeError > 0
            ? parseFloat((sumCorrectBeforeError / countCorrectBeforeError).toFixed(2))
            : 0;

        const averageErrorBeforeCorrect =
          countErrorBeforeCorrect > 0
            ? parseFloat((sumErrorBeforeCorrect / countErrorBeforeCorrect).toFixed(2))
            : 0;

        // 记录当前的连续状态
        let currentStreakType = null;
        let currentStreakLength = 0;

        if (currentErrorStreak > 0) {
          currentStreakType = '错误';
          currentStreakLength = currentErrorStreak;
        } else if (currentCorrectStreak > 0) {
          currentStreakType = '正确';
          currentStreakLength = currentCorrectStreak;
        }

        // 计算当前连续状态与平均值的对比
        let streakComparisonToAverageCorrectBeforeError = null;
        let streakComparisonToAverageErrorBeforeCorrect = null;

        if (currentStreakType === '正确') {
          if (averageCorrectBeforeError > 0) {
            if (currentStreakLength > averageCorrectBeforeError) {
              streakComparisonToAverageCorrectBeforeError = '高于平均水平';
            } else if (currentStreakLength < averageCorrectBeforeError) {
              streakComparisonToAverageCorrectBeforeError = '低于平均水平';
            } else {
              streakComparisonToAverageCorrectBeforeError = '等于平均水平';
            }
          } else {
            streakComparisonToAverageCorrectBeforeError = '无平均水平数据';
          }
        }

        if (currentStreakType === '错误') {
          if (averageErrorBeforeCorrect > 0) {
            if (currentStreakLength > averageErrorBeforeCorrect) {
              streakComparisonToAverageErrorBeforeCorrect = '高于平均水平';
            } else if (currentStreakLength < averageErrorBeforeCorrect) {
              streakComparisonToAverageErrorBeforeCorrect = '低于平均水平';
            } else {
              streakComparisonToAverageErrorBeforeCorrect = '等于平均水平';
            }
          } else {
            streakComparisonToAverageErrorBeforeCorrect = '无平均水平数据';
          }
        }

        return {
          averageConsecutiveErrors,
          averageConsecutiveCorrects,
          averageCorrectBeforeError,
          averageErrorBeforeCorrect,
          totalErrors,
          totalCorrects,
          totalErrorStreaks,
          totalCorrectStreaks,
          currentStreakType,
          currentStreakLength,
          streakComparisonToAverageCorrectBeforeError,
          streakComparisonToAverageErrorBeforeCorrect
        };
      }

      const metrics = calculateMetrics(values);
      console.log('平均连续错误次数:', metrics.averageConsecutiveErrors);
      console.log('平均连续正确次数:', metrics.averageConsecutiveCorrects);
      console.log('平均连续正确多少次之后可能会错误:', metrics.averageCorrectBeforeError);
      console.log('平均连续错误多少次之后可能会正确:', metrics.averageErrorBeforeCorrect);
      console.log('总错误次数:', metrics.totalErrors);
      console.log('总正确次数:', metrics.totalCorrects);
      console.log('总错误段数:', metrics.totalErrorStreaks);
      console.log('总正确段数:', metrics.totalCorrectStreaks);
      console.log('当前连续类型:', metrics.currentStreakType);
      console.log('当前连续长度:', metrics.currentStreakLength);
      console.log('当前连续正确与平均正确前错误对比:', metrics.streakComparisonToAverageCorrectBeforeError);
      console.log('当前连续错误与平均错误前正确对比:', metrics.streakComparisonToAverageErrorBeforeCorrect);
      console.log('总数验证 (应等于values.length):', metrics.totalErrors + metrics.totalCorrects === values.length);
    </script>
  </body>
</html>
